﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace CustomControls
{
    public class DataForGrid
    {
        #region VisibleColumns property
        private Hashtable _VisibleColumns = new Hashtable();
        public Hashtable VisibleColumns
        {
            get
            {
                return _VisibleColumns;
            }
            set
            {
                _VisibleColumns = value;

            }
        }
        #endregion

        #region ValueColumn property
        private string _ValueColumn;
        public string ValueColumn
        {
            get
            {
                return _ValueColumn;
            }
            set
            {
                _ValueColumn = value;
            }
        }
        #endregion

        #region DisplayColumn property
        private string _DisplayColumn;
        public string DisplayColumn
        {
            get
            {
                return _DisplayColumn;
            }
            set
            {
                _DisplayColumn = value;
            }
        }
        #endregion

        #region SortColumn property
        private string _SortColumn = "";
        public string SortColumn
        {
            get
            {
                return _SortColumn;
            }
            set
            {
                _SortColumn = value;
            }
        }
        #endregion

        #region DataSource property
        private DataView _DataSource;
        public DataView DataSource
        {
            get
            {
                return _DataSource;
            }
            set
            {
                if (value != null)
                {
                    _DataSource = value.ToTable().DefaultView;
                    foreach (DataColumn col in _DataSource.Table.Columns)
                    {
                        col.AllowDBNull = true;
                        col.Unique = false;
                    }
                    for (int i = 0; i < _DataSource.Table.Rows.Count; i++)
                    {
                        if (_DataSource.Table.Rows[i].IsNull("ID"))
                        {
                            _DataSource.Table.Rows.RemoveAt(i);
                            i--;
                        }
                    }

                    _DataSource.Table.Rows.Add(_DataSource.Table.NewRow());
                    if (!string.IsNullOrEmpty(_DataSource.RowFilter))
                    {
                        _DataSource.RowFilter = _DataSource.RowFilter + " OR ID IS NULL";
                    }
                    _DataSource.Sort = value.Sort;

                }
                else
                {
                    _DataSource = value;
                }
            }
        }

        #endregion

        #region EditValue property
        private object _EditValue;
        public object EditValue
        {
            get
            {
                return _EditValue;
            }
            set
            {
                _EditValue = value;
            }
        }
        #endregion

        public DataForGrid() { }
        public DataForGrid(Hashtable HtDataForGrid, DataView DataSource, string key)
        {
            this.DataSource = DataSource;
            if (DataSource.Table.Columns.Contains("Ma"))
            {
                this.AddVisibleColumn("Ma", "Mã số", 100, 1);
            }
            else
            {
                this.AddVisibleColumn("SoHieu", "Số hiệu", 100, 1);
            }
            this.AddVisibleColumn("Ten", "Tên", 300, 2);
            this.DisplayColumn = "Ten";
            this.ValueColumn = "ID";
            HtDataForGrid.Add(key, this);
        }
        public DataForGrid(Hashtable HtDataForGrid, DataView DataSource, string key, string DisplayColumn)
        {
            this.DataSource = DataSource;
            if (DataSource.Table.Columns.Contains("Ma"))
            {
                this.AddVisibleColumn("Ma", "Mã số", 100, 1);
            }
            else
            {
                this.AddVisibleColumn("SoHieu", "Số hiệu", 100, 1);
            }
            this.AddVisibleColumn("Ten", "Tên", 300, 2);
            this.DisplayColumn = DisplayColumn;
            this.ValueColumn = "ID";
            HtDataForGrid.Add(key, this);
        }

        public void AddVisibleColumn(string columnName, string caption)
        {
            if (!_VisibleColumns.ContainsKey(columnName))
            {
                InfoOfColumn info = new InfoOfColumn();
                info.VisibleIndex = _VisibleColumns.Count + 1;
                info.Caption = caption;
                info.ColumnWidth = -1;
                _VisibleColumns.Add(columnName, info);
            }
        }
        public void AddVisibleColumn(string columnName, string caption, int columnWidth)
        {
            if (!_VisibleColumns.ContainsKey(columnName))
            {
                InfoOfColumn info = new InfoOfColumn();
                info.VisibleIndex = _VisibleColumns.Count + 1;
                info.Caption = caption;
                info.ColumnWidth = columnWidth;
                _VisibleColumns.Add(columnName, info);
            }
        }
        public void AddVisibleColumn(string columnName, string caption, int columnWidth, int visibleIndex)
        {
            if (!_VisibleColumns.ContainsKey(columnName))
            {
                InfoOfColumn info = new InfoOfColumn();
                info.VisibleIndex = visibleIndex;
                info.Caption = caption;
                info.ColumnWidth = columnWidth;
                _VisibleColumns.Add(columnName, info);
            }
        }
        public void AddVisibleColumn(string columnName, string caption, int columnWidth, int visibleIndex, bool filterBy)
        {
            if (!_VisibleColumns.ContainsKey(columnName))
            {
                InfoOfColumn info = new InfoOfColumn();
                info.VisibleIndex = visibleIndex;
                info.Caption = caption;
                info.ColumnWidth = columnWidth;
                info.FilterBy = filterBy;
                _VisibleColumns.Add(columnName, info);
            }
        }
    }
}
